﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using ClassLibrary.Entity;
using ClassLibrary.DAO;
using ClassLibrary.Data;


public partial class UserControl_ucLoanPhone : System.Web.UI.UserControl
{
    TblLoan2PhoneDAO Loan2PhoneDAO = new TblLoan2PhoneDAO(Global.CONNECTION_STRING);
    TblLoanPhoneDAO LoanPhoneDAO = new TblLoanPhoneDAO(Global.CONNECTION_STRING);
    TblJobsDAO jobDAO = new TblJobsDAO(Global.CONNECTION_STRING);
   
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

            string id = Request.QueryString["id"];

            if (id != null)
            {
                LoadLoanPhone();
            }
            else
            {
                LoadLoanPhone();
            }            
        }

    }

    private void LoadLoanPhone()
    {
        drPhoneID.DataSource = Loan2PhoneDAO.LoadIDLPhone();
        drPhoneID.DataTextField = "LPhoneID";
        drPhoneID.DataValueField = "LPhoneID";
        drPhoneID.DataBind();
        drPhoneID.Items.Insert(0, new ListItem("", ""));
        drPhoneID.SelectedIndex = -1;
        
    }

 
    protected void btnSave_Click(object sender, EventArgs e)
    {
        int id = Global.ConvertToInt(Request.QueryString["id"]);
        bool Flag = false;
        try
        {
           // int idCus = ((TblCustomers)Session[Global.SESSION_LOGGED_USER]).CustomerIndex;
            TblLoanPhone loanphone = new TblLoanPhone();
           
            DataTable dtCus = jobDAO.GetJob(id, "");
            
            foreach (DataRow r in dtCus.Rows)
            {
                loanphone.CustomerID = Global.ConvertToInt(r["CustomerIndex"].ToString());
                
            }
            loanphone.LPhoneID = drPhoneID.SelectedValue;
            loanphone.Notes = txt_notes.Text;
            loanphone.PaymentMethod = dropMethod.SelectedItem.Text;
            loanphone.Brand = txt_Brand.Text;
            loanphone.Model = txt_model.Text;
            loanphone.IMENo = txt_IME.Text;
            loanphone.PaymentRef = "";

            loanphone.LoanFee = Convert.ToDecimal(txt_loanfee.Text);
            loanphone.AccessoriesIncluded = txt_access.Text;
            loanphone.AmountDeposited = Convert.ToDecimal(txt_amount.Text);

            loanphone.DateIn = DateTime.Now;
            loanphone.DateOut = DateTime.Now;

            if (cbDepositeTaken.Checked == true)
            {
                loanphone.DepositeTaken = Convert.ToBoolean(1);
                
            }
            else
            {
                loanphone.DepositeTaken = Convert.ToBoolean(1);
            }
            if (cbDesRef.Checked == true)
            {
                loanphone.DepositeRefunded = Convert.ToBoolean(1);
            }
            else
            {
                loanphone.DepositeRefunded = Convert.ToBoolean(0);
            }
            loanphone.LoanID = 0;
            loanphone.JobID = id;

            LoanPhoneDAO.InsUpd_TblLoanPhone(loanphone, 1);

            Loan2PhoneDAO.Update(1, drPhoneID.SelectedValue);


            Flag = true;
            
        }
        catch (Exception ex)
        {
            Session[Global.SESSION_ERROR] = ex;
            Response.Redirect(Global.ERROR_PAGE);
        }

        if (Flag == true)
        {
            LoadLoanPhone();
            //Response.Redirect("~/Booking.aspx?id=" +id);
            ScriptManager.RegisterStartupScript(btnSave, btnSave.GetType(), "ScriptLoginSuccess", "UpdateParentsLogin();", true);
        }

    }

    protected void drPhoneID_SelectedIndexChanged(object sender, EventArgs e)
    {
        DataTable L2Phone_dt = Loan2PhoneDAO.GetLPhoneByID(drPhoneID.SelectedValue);
        foreach (DataRow dr in L2Phone_dt.Rows)
        {
            txt_Brand.Text = dr["LPhoneBrand"].ToString();
            txt_model.Text = dr["LPhoneModel"].ToString();
            txt_IME.Text = dr["LPIMENo"].ToString();
            txt_access.Text = dr["AccessoriresIncluded"].ToString();
            txt_amount.Text = dr["Deposit"].ToString();
            txt_loanfee.Text = dr["LoanFee"].ToString();
        }
    }
}
